<template>
  <div id="main">
    <el-form ref="addform" :model="addform" :rules="fromrules">

      <el-form-item label="数据源" label-width="120px" prop="datasourceId">
        <el-select filterable style="width: 90%" size="mini" v-model="addform.datasourceId"
                   placeholder="请选择数据源">
          <el-option
            v-for="item in dataSourceOptions"
            :key="item.id"
            :label="item.datasourceName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否使用mybatis语义" label-width="140px" prop="enableMybatis">
        <el-radio-group v-model="addform.enableMybatis">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否开启事务" label-width="140px" prop="enableTransaction">
        <el-radio-group v-model="addform.enableTransaction">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="延迟" label-width="120px" prop="">
        <span style="color: #FFFFFF;font-size: 12px">
          <el-input style="width: 10%" size="mini" v-model="addform.preWaitTime" autocomplete="off"></el-input>
          <span style="margin-left: 10px">毫秒执行</span></span>
      </el-form-item>
      <el-form-item label="sql文本" label-width="120px" prop="sqlText">
        <el-input type="textarea" :rows="10" style="width: 90%"
                  placeholder="支持mybaits表达式，<需要使用&lt;转义 ,多个语句用;分隔" size="mini"
                  v-model="addform.sqlText"
                  autocomplete="off"></el-input>
      </el-form-item>
    </el-form>

  </div>
</template>
<script>
import { codemirror } from "vue-codemirror";
import "codemirror/lib/codemirror.css"; // 基本样式
import "codemirror/theme/ambiance.css"; // 主题1 这里引入的是主题样式，根据设置的theme的主题引入，一定要引入！！
import "codemirror/theme/base16-light.css"; // 主题2
import "codemirror/theme/cobalt.css"; // 主题3
import "codemirror/mode/sql/sql.js";
import Crontab from "@/views/taskscheduler/components/crontab/index.vue";
//禁止缩放
export default {
  // components: {
  //   Crontab,
  //   codemirror
  // },
  data() {
    return {
      dataSourceOptions: [],
      addform: {
        datasourceId: "",
        sqlText: "",
        enableMybatis: 0,
        enableTransaction: 0,
        preWaitTime: ""
      },
      fromrules: {
        datasourceId: { required: true, message: "请输入", trigger: "blur" },
        sqlText: { required: true, message: "请选择", trigger: "change" },
        enableMybatis: { required: true, message: "请选择", trigger: "change" },
        enableTransaction: { required: true, message: "请选择", trigger: "change" }
      }
    };
  },
  methods: {
    getdata() {
      // console.log("data", this.form);
    },
    getFormOption() {
      return this.$http.post("/offlineSync/findDatasource").then((res) => {
        this.dataSourceOptions = res.data;
      });
    },
    invertData(data) {
      if (!data) {
        this.addform = {
          datasourceId: "",
          sqlText: "",
          enableMybatis: 0,
          enableTransaction: 0,
          preWaitTime: ""
        };
        return;
      }
      Promise.all([this.getFormOption()]).then((res) => {
      }).finally(() => {
        this.$nextTick(() => {
          if (data) {
            this.addform = data;
            if (!data.enableMybatis) {
              this.addform.enableMybatis = 0;
            }else {
              this.addform.enableMybatis = Number(data.enableMybatis);
            }
            if (!data.enableTransaction) {
              this.addform.enableTransaction = 0;
            }else {
              this.addform.enableTransaction = Number(data.enableTransaction);
            }
            }
          this.addform.datasourceId = Number(data.datasourceId);
        });

      });
    },
    restForm() {
      this.$refs.addform.resetFields();
    }
  },
  mounted() {
    this.getFormOption();
  }

};
</script>
<style lang="scss" scoped>
::v-deep.code-mirror {
  .CodeMirror-line {
    font-size: 16px;
  }

  .CodeMirror-linenumber {
    font-size: 16px;
  }

  .cm-s-cobalt.CodeMirror {
    background-color: black;
  }
}
</style>
